from pydantic import BaseModel
from typing import Any, Generic, TypeVar, Union
from pydantic.generics import GenericModel

# 定义泛型类型T
T = TypeVar('T')


# 定义返回结果的通用模型
class Result(GenericModel, Generic[T]):
    code: int  # 返回码
    message: str  # 返回信息
    data: Union[T, None] = None  # 返回数据，可以为任意类型或None

    @classmethod
    def ok(cls, data: Any = None, message: str = "成功"):
        return cls(code=200, message=message, data=data)

    @classmethod
    def error(cls, message: str = "错误"):
        return cls(code=400, message=message)
